package com.angel.web.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.MD5Util;
import com.angel.core.util.Pager;
import com.angel.core.util.ResultParam;
import com.angel.core.util.StringUtils;
import com.angel.health.SysUser;
import com.angel.health.SysUserExt;
import com.angel.provider.sys.SysManagerUserProvider;

/**
 * 高亚林
 * 2016年9月20日下午1:03:22
 */

@RequestMapping("/manag/user")
@Controller
public class SysManagerUserController {

	@Autowired
	private SysManagerUserProvider provider;
	
	//查询所有用户
	@RequestMapping("/findAllUser")
	@ResponseBody
	public String findAllUser(){
		List<SysUser> list = provider.findAllUser();
		return JsonUtil.toJson(list);		
	}
	
	//分页查询
	@RequestMapping("/index")
	public String index(SysUser user,Pager pagers,HttpServletRequest request){
		String url = request.getRequestURL().toString();
		Integer param = user.getOfficeId();
		if(param != null){
			url += "?officeId="+ param;
		}
		System.out.println(url);
		Pager pager = provider.findAll(user, pagers);
		request.setAttribute("pager", pager);
		request.setAttribute("url", url);
		return "manag/user";
	}
	
	//批量删除
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String ids){
		ResultParam param = provider.delete(ids);
		return JsonUtil.toJson(param);
	}
	
	//删除单个用户
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(SysUser user){
		ResultParam param = provider.deleteById(user);
		return JsonUtil.toJson(param);
	}
	
	//保存
	@RequestMapping("/save")
	@ResponseBody
	public String save(SysUser user,String role,HttpServletRequest request){
		Map<String, String> map = new HashMap<String, String>();
		if(user == null || user.getNo() == null || user.getNo() == ""){
			map.put("msg", "工号不能为空");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(user.getName() == null || "".equals(user.getName())){
			map.put("msg", "姓名不能为空");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		SysUser user1 = new SysUser();
		user1.setName(user.getName());
		List<Map<String, Object>> list = provider.findAllUsers(user1);
		if(list != null && list.size() > 0){
			map.put("msg", "姓名已存在");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(user.getLoginName() == null || "".equals(user.getLoginName())){
			map.put("msg", "用户名不能为空");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}else if(user.getLoginName().length() < 6 || user.getLoginName().length() > 30){
			map.put("msg", "用户名字符长度要大于6位小于30位");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		SysUser user2 = new SysUser();
		user2.setLoginName(user.getLoginName());
		List<Map<String, Object>> list2 = provider.findAllUsers(user2);
		if(list2 != null && list2.size() > 0){
			map.put("msg", "用户名已存在");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(user.getPassword() == null || "".equals(user.getPassword())){
			map.put("msg", "密码不能为空");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(!user.getPassword().equals(user.getConfirmPassword())){
			map.put("msg", "密码不一致");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(StringUtils.isEmpty(user.getEmail())){
			map.put("msg", "邮箱不能为空");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		SysUser user3 = new SysUser();
		user3.setEmail(user.getEmail());
		List<Map<String, Object>> list3 = provider.findAllUsers(user3);
		if(list3 != null && list3.size() > 0){
			map.put("msg", "邮箱已存在");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		if(StringUtils.isEmpty(user.getMobile())){
			map.put("msg", "手机号不能为空");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		SysUser user4 = new SysUser();
		user4.setMobile(user.getMobile());
		List<Map<String, Object>> list4 = provider.findAllUsers(user4);
		if(list4 != null && list4.size() > 0){
			map.put("msg", "手机号已存在");
			map.put("code", "0");
			return JsonUtil.toJson(map);
		}
		user.setPassword(MD5Util.getMD5(user.getPassword()));
		user.setCreateBy(request.getSession().getAttribute("user").toString());
		int i = provider.save(user,role);
		if(i > 0){
			map.put("msg", "添加成功");
			map.put("code", "1");
			return JsonUtil.toJson(map);
		}
		map.put("msg", "添加失败");
		map.put("code", "0");
		return JsonUtil.toJson(map);
	}
	
	//根据id获取数据
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(SysUser user){
		List<SysUserExt> list = provider.findUserById(user);
		if(list != null && list.size() > 0){
			SysUserExt ext = list.get(0);
			return JsonUtil.toJson(ext);
		}
		return "";
	}
	
	//修改
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, String> update(SysUser user,String role,HttpServletRequest request){
		user.setUpdateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.update(user,role);
		return map;
	}
	
	//修改图像
	@RequestMapping("/updatePhoto")
	@ResponseBody
	public String updatePhoto(SysUser user){
		Map<String, String> map = new HashMap<String, String>();
		try {
			if(StringUtils.isEmpty(user.getPhoto())){
				map.put("code", "0");
				map.put("msg", "请更换图像");
				return JsonUtil.toJson(map);
			}
			provider.updatePhoto(user);
		} catch (Exception e) {
			map.put("code", "0");
			map.put("msg", "修改图像失败");
			e.printStackTrace();
		}
		map.put("code", "1");
		map.put("msg", "修改图像成功");
		return JsonUtil.toJson(map);
	}
}
